package com.streetsmart;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.streetsmart.Utilities;

public class MapRouteActivity extends com.google.android.maps.MapActivity {
	private MapView mapView;
	private List<Overlay> mapOverlays;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.route);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapOverlays = mapView.getOverlays();

		Bundle extras = getIntent().getExtras();
		/*
		 * if(extras == null) { Utilities.clearGeopoints(); initGeoPoints(); }
		 */// ---- > incapcitating the testign environment here

		// set of custom pins used for laying n maps
		ArrayList<Drawable> markers = new ArrayList<Drawable>();
		ArrayList<Integer> ids = new ArrayList<Integer>();
		ids.add(R.drawable.green01);
		ids.add(R.drawable.green02);
		ids.add(R.drawable.green03);
		ids.add(R.drawable.green04);
		ids.add(R.drawable.green05);
		ids.add(R.drawable.green06);
		ids.add(R.drawable.green07);
		ids.add(R.drawable.green08);
		ids.add(R.drawable.green09);

		for (int count = 0; count < ids.size(); count++) {
			markers.add(this.getResources().getDrawable(ids.get(count)));
		}

		Drawable drawable = this.getResources().getDrawable(R.drawable.green01);
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
				drawable, this);
		for (int count = 0; count < Utilities.latitudeList.size(); count++) {
			GeoPoint point = new GeoPoint(
					(int) (Utilities.latitudeList.get(count) * 1E6),
					(int) (Utilities.longitudeList.get(count) * 1E6));
			double[] geocodes = { Utilities.latitudeList.get(count),
					Utilities.longitudeList.get(count) };
			String addressOfPoint = "";
			try {
				addressOfPoint = Utilities.getAddress(geocodes);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String place = "stop at";
			if (count == 0) {
				place = "Source";
			}
			if (count == Utilities.latitudeList.size() - 1) {
				place = "Destination";
			}
			OverlayItem overlayitem = new OverlayItem(point, place,
					addressOfPoint);
			Drawable marker = markers.get(count);
			marker.setBounds(0, 0, marker.getIntrinsicWidth(),
					marker.getIntrinsicHeight());
			overlayitem.setMarker(marker);
			itemizedoverlay.addOverlay(overlayitem);
		}

		mapOverlays.add(itemizedoverlay);
		MapController mc = mapView.getController();

		double minLat = Integer.MAX_VALUE;
		double minLong = Integer.MAX_VALUE;
		double maxLat = Integer.MIN_VALUE;
		double maxLong = Integer.MIN_VALUE;

		for (int i = 0; i < Utilities.latitudeList.size(); i++) {
			// GeoPoint point : pointsList) {
			double latPoint = Utilities.latitudeList.get(i) * 1E6;
			double lngPoint = Utilities.longitudeList.get(i) * 1E6;
			minLat = Math.min(latPoint, minLat);
			minLong = Math.min(lngPoint, minLong);
			maxLat = Math.max(latPoint, maxLat);
			maxLong = Math.max(lngPoint, maxLong);
		}

		// mc.animateTo(new GeoPoint((maxLat + minLat) / 2,
		// (maxLong + minLong) / 2));

		// mc.setCenter(new GeoPoint((int)(Utilities.latitudeList.get(0) * 1E6),
		// (int)(Utilities.longitudeList.get(0) * 1E6)));
		mc.setCenter(new GeoPoint((int) (maxLat + minLat) / 2,
				(int) (maxLong + minLong) / 2));
		mc.zoomToSpan((int) Math.abs(minLat - maxLat),
				(int) Math.abs(minLong - maxLong));
		// mc.setZoom(12);

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	public static void initGeoPoints() {
		Utilities.latitudeList.add(37.72347850);
		Utilities.latitudeList.add(37.77628960);
		Utilities.latitudeList.add(37.76997880);
		Utilities.latitudeList.add(37.81958640);
		Utilities.longitudeList.add(-122.47852560);
		Utilities.longitudeList.add(-122.41815040);
		Utilities.longitudeList.add(-122.46628770);
		Utilities.longitudeList.add(-122.47853250);
	}

}